<?php
/**
 * adServer.class.php
 *
 * @package adServer
 */
/* Load settings and includes */
require_once ('./settings/siteConfig.php');
require_once (CLASS_PATH .'classDatabase.php');
require_once (CLASS_PATH .'dbStatements.php');

class adServer {
    public $dbHandle;
     /* Hold the handle to the db instance */

    /**
     * Constructor
     */
    function adServer() {
        /* Create the DB connection */
        $this->dbHandle = new database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, false);

        /*try to select the database */
        if ($this->dbHandle->selectDb(DB_NAME) != true) {
            /* DB is not setup, need to be setup */
            echo "FATAL ERROR: Application is not setup yet, please run index.php";
        }

    }

    /**
     * Returns the next adunit in line (the one with the least hit witin the criterias) 
     * 
     * @param integer $adType
     * @param string $adLanguage
     * @param string $adCampaign
     * @return 
     */
    function getNextAdUnit($adType, $adLanguage, $adCampaign) {
        $adType = mysql_real_escape_string($adType);
        $adLanguage = $adLanguage == '--' ? '%' : mysql_real_escape_string($adLanguage);
        $adCampaign = $adCampaign == 'Default' ? '%' : mysql_real_escape_string($adCampaign);
        if($adType='all')
            $qry=SQL_GET_AD_BY_CRITERIA_NOTYPE;
        else
            $qry= SQL_GET_AD_BY_CRITERIA;   
        $qry = str_replace('%%TYPEID%%', $adType, $qry);
        $qry = str_replace('%%LANGUAGE%%', $adLanguage, $qry);
        $qry = str_replace('%%CAMPAIGN%%', $adCampaign, $qry);

        $results = $this->dbHandle->get_results($qry);
        if ($results) {
            /* Increment display counter for first ad */
            $qry = str_replace('%%ADUNITID%%', $results[1]['adUnitId'],
                SQL_INCREMENT_AD_COUNTER);
            $qry = str_replace('%%NEWCOUNT%%', $results[1]['adDisplayedCount'] +
                1, $qry);
            $this->dbHandle->qry($qry);
            $results[1]['adHtml'] = stripslashes($results[1]['adHtml']);
            return $results[1];
        } else {
            $results = array('adUnitId' => '0', 'adTypeId' => '1', 'adDuration' =>
                '2', 'adStartOffsetPercent' => '0', 'adHtml' =>
                '<span style="color:red;font-weight:bold;text-align:center;font-size:250%;margin-top:50px;">' .
                "Ad Not Found</span><br/>Criteria: type:$adType, Campaign:$adCampaign, Language:$adLanguage");
            return $results;
        }

    }

    /**
     * Return the type Name from the type Id
     * @param integer $adType
     * @return 
     */
    function getTypenameById($adType) {
        $adType = mysql_real_escape_string($adType);
        $qry = str_replace('%%TYPEID%%', $adType, SQL_GET_TYPENAME_BY_ID);
        $results = $this->dbHandle->get_results($qry);
        if ($results) {
            return $results[1]['typeName'];
        } else {
            return false;
        }

    }

}

?>